package com.dymall.payment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dymall.common.util.UserContext;
import com.dymall.payment.domain.pojo.CreditCard;
import com.dymall.payment.domain.dto.CreditCardInfo;
import com.dymall.payment.mapper.CreditCardMapper;
import com.dymall.payment.service.ICreditCardService;
import org.apache.commons.validator.routines.CreditCardValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class CreditCardServiceImpl extends ServiceImpl<CreditCardMapper, CreditCard> implements ICreditCardService {
    @Autowired
    private CreditCardMapper creditCardMapper;

    @Autowired
    private CreditCardValidator creditCardValidator;

    @Transactional
    @Override
    public void bindCreditCard(CreditCardInfo cardInfo) {
        if(creditCardValidator.isValid(cardInfo.getCardNum())){
            creditCardMapper.insertCard(UserContext.getUser(),cardInfo);
        }else {
            throw new IllegalArgumentException("无效的卡号");
        }
    }
}
